import os

from src.GameConfig import *


class BackGround:

    def __init__(self):
        super().__init__()
        print("===相对路径====",os.getcwd())
        self.bg1 = BackGroundSprite(BASE_RESOURCE_PATH_IMAGE+"background.png")
        self.bg2 = BackGroundSprite(BASE_RESOURCE_PATH_IMAGE+"background.png")
        self.bg2.rect.y = -self.bg2.rect.height  # 定义2张图片上下排列实现连续滚动
        self.back_group = pygame.sprite.Group((self.bg1, self.bg2))

    def step(self, step):
        self.back_group.update()

    def onDraw(self, canvas):
        self.back_group.draw(canvas)


class BackGroundSprite(pygame.sprite.Sprite):

    def __init__(self, image_name, speed=1):
        super().__init__()
        self.image = pygame.image.load(image_name)
        self.rect = self.image.get_rect()  # 定义位置
        self.speed = speed

    def update(self):
        # 在屏幕垂直位置
        self.rect.y += self.speed
        # 如果移动超过了屏幕，将图像重置到屏幕的上方
        if self.rect.y >= SCREEN_RECT.height:
            self.rect.y = -self.rect.height
